package com.code.generate.gen;

import com.code.generate.Field;
import com.code.generate.enums.ControlType;

public class FieldCodeGenerator implements GeneratorAble {
    private final Field field;
    private final String requestObjectName;
    private final String priffSpace;
    private String moduleShorName = "";

    public FieldCodeGenerator(Field field, String requestObjectName, String moduleShorName) {
        this.field = field;
        this.requestObjectName = requestObjectName;
        this.moduleShorName = moduleShorName;
        this.priffSpace = "             ";
    }

    public FieldCodeGenerator(Field field, String requestObjectName, String moduleShorName, String priffSpace) {
        this.field = field;
        this.requestObjectName = requestObjectName;
        this.moduleShorName = moduleShorName;
        this.priffSpace = priffSpace;
    }

    private String generateHtml(String tag, boolean isHead) {
        StringBuilder sb = new StringBuilder();
        sb.append(priffSpace);
        sb.append("{type:'label',value:'" + field.getFieldCName() + "'},\n");
        sb.append(priffSpace);
        if (isHead) sb.append("[");
        if (field.getInputType() == null) {
            sb.append("'${").append(requestObjectName).append(".");
            sb.append(field.getFieldName());
            sb.append("?if_exists}'");
        } else if (ControlType.DATE.equals(field.getInputType()) || ControlType.DATE_TIME.equals(field.getInputType())
                || ControlType.TIMESTAMP.equals(field.getInputType()) || ControlType.TIME.equals(field.getInputType())) {
            sb.append("<@date id=\"" + field.getFieldName() + tag + "\" name=\"" + field.getFieldName()
                    + "\" value=\"${" + requestObjectName + "." + field.getFieldName() + "}\"");
            if (field.isWhetherRequest()) {
                sb.append(" parameter=\"{need:true}\"");
            }
            sb.append(" type=\"");
            if (ControlType.TIMESTAMP.equals(field.getInputType())) {
                sb.append("dateTime");
            } else {
                sb.append(field.getInputType().getValue());
            }
            sb.append("\"/>");
        } else if (ControlType.CHECKBOX.equals(field.getInputType()) || ControlType.SELECT.equals(field.getInputType())
                || ControlType.SELECT_CHECKBOX.equals(field.getInputType())
                || ControlType.SELECT_TREE.equals(field.getInputType())
                || ControlType.SELECT_TREE_CHECKBOX.equals(field.getInputType())
                || ControlType.SELECT_TREE_RADIO.equals(field.getInputType())
                || ControlType.RADIO.equals(field.getInputType())) {
            sb.append("<@widget module=\"\" type=\"" + field.getInputType().getValue() + "\" id=\""
                    + field.getFieldName() + tag + "\" name=\"" + field.getFieldName() + "\" value="
                    + requestObjectName + "." + field.getFieldName());
            if (field.isWhetherRequest()) {
                sb.append(" parameter=\"{need:true}\"");
            }
            sb.append("/>");
        } else if (ControlType.FILE_UPLOAD.equals(field.getInputType())
                || ControlType.IMAGE_UPLOAD.equals(field.getInputType())) {
            sb.append("<@fileUpload module=\"" + moduleShorName + "\"  id=\"" + field.getFieldName() + tag
                    + "\" name=\"" + field.getFieldName() + "\" value=" + requestObjectName + "."
                    + field.getFieldName() + " type=\"");
            if (ControlType.IMAGE_UPLOAD.equals(field.getInputType())) {
                sb.append("image\"");
            } else {
                sb.append("file\"");
            }
            if (field.isWhetherRequest()) {
                sb.append(" parameter=\"{need:true}\"");
            }
            sb.append("/>");
        } else if (ControlType.WEB_EDITOR.equals(field.getInputType())) {
            sb.append("<@webEditor name=\"" + field.getFieldName() + "\" id=\"" + field.getFieldName() + tag
                    + "\" value=" + requestObjectName + "." + field.getFieldName());
            if (field.isWhetherRequest()) {
                sb.append(" parameter=\"{need:true}\"");
            }
            sb.append("/>");
        } else if (ControlType.STATIC_SELECT.equals(field.getInputType())) {
            sb.append("{type:'select'");
            if (field.isWhetherRequest()) {
                sb.append(",need:true");
            }
            sb.append(",id:'").append(field.getFieldName() + tag).append("'");
            sb.append(",name:'").append(field.getFieldName()).append("'");
            sb.append(",value:[\n");
            sb.append(priffSpace).append("  {value:'',text:'选择'},\n");
            sb.append(priffSpace).append("  {value:'value1',text:'text1'<#if " + requestObjectName + "."
                                                 + field.getFieldName() + "?exists&&" + requestObjectName + "."
                                                 + field.getFieldName()
                                                 + "?string==\"value1\">, selected:true</#if>},\n");
            sb.append(priffSpace).append("  {value:'value2',text:'text2'<#if " + requestObjectName + "."
                                                 + field.getFieldName() + "?exists&&" + requestObjectName + "."
                                                 + field.getFieldName() + "?string==\"value2\">, selected:true</#if>}");
            sb.append("]}");
        } else if (ControlType.STATIC_RADIO.equals(field.getInputType())
                || ControlType.STATIC_CHECKBOX.equals(field.getInputType())) {
            sb.append("[\n");
            sb.append(priffSpace);
            sb.append("  {type:'").append(field.getInputType().getValue()).append("'");
            if (field.isWhetherRequest()) {
                sb.append(",need:true");
            }
            sb.append(",name:'").append(field.getFieldName()).append("'");
            sb.append(",value:'value1',text:'text1'");
            sb.append("<#if " + requestObjectName + "." + field.getFieldName() + "?exists&&" + requestObjectName + "."
                    + field.getFieldName() + "?string==\"value1\">, checked:true</#if>},\n");
            sb.append(priffSpace);
            sb.append("  {type:'").append(field.getInputType().getValue()).append("'");
            sb.append(",name:'").append(field.getFieldName()).append("'");
            sb.append(",value:'value2',text:'text2'");
            sb.append("<#if " + requestObjectName + "." + field.getFieldName() + "?exists&&" + requestObjectName + "."
                    + field.getFieldName() + "?string==\"value2\">, checked:true</#if>}\n");
            sb.append(priffSpace).append("]");
        } else {
            sb.append("{type:'" + field.getInputType().getValue() + "'");
            if (field.isWhetherRequest()) {
                sb.append(",need:true");
            }
            sb.append(",id:'").append(field.getFieldName() + tag).append("'");
            sb.append(",name:'").append(field.getFieldName()).append("'");
            if (ControlType.TEXTAREA.equals(field.getInputType())) {
                sb.append(",value:'${stringTransfer(" + requestObjectName + "." + field.getFieldName()
                        + "?if_exists)}'");
            } else {
                sb.append(",value:'${" + requestObjectName + "." + field.getFieldName() + "?if_exists}'");
            }
            sb.append("}");
        }
        if (isHead)
            sb.append("]\n");
        else
            sb.append("\n");
        return sb.toString();
    }

    @Override
    public String generateSearchHtml(boolean isHead) {
        return generateHtml("_search", isHead);
    }

    @Override
    public String generateEditHtml() {
        return generateHtml("", false);
    }
}
